package com.virgo.common.utils;

import lombok.experimental.UtilityClass;

import java.util.*;

@UtilityClass
public class ICommonUtils {

	final private String[] randomString = new String[]{
			"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
			"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
			"Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
			"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
			"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
			"y", "z"
	};


	/**
	 * 生成整数的随机数
	 * @param min 最小数
	 * @param max 最大数
	 * @return int 随机数
	 */
	public int getRandom(int min,int max) {
		Random random = new Random();
		return random.nextInt(max - min + 1) + min;
	}

	/**
	 * 生成订单号
	 * @param prefix 前缀
	 * @return String
	 */
	public String getOrderSn(String prefix) {
		String nowTime = ITimeUtils.localDate("yyyyMMddHHmmss");
		int randNum = getRandom(1000,9999);
		return prefix + nowTime + randNum;
	}

	/**
	 * 字符串转数字数组 但是只要结果大于0的、用于删除数据库记录的时候
	 * @param str 字符串
	 * @param regex 分割符号
	 * @return Integer[]
	 */
	public Long[] stringToArray(String str, String regex) {
		LinkedList<Long> result = new LinkedList<>();
		String[] data = str.split(regex);
		for (String temp : data) {
			long tempId = Long.parseLong(temp);
			if (tempId > 0) {
				result.add(tempId);
			}
		}
		return result.toArray(new Long[0]);
	}

	/**
	 * 获取指定长度的随机字符串
	 * @param charCount 长度
	 * @return 生成的字符串
	 */
	public String getRandomString(int charCount) {
		if (charCount < 1) {
			return "";
		}
		StringBuilder result = new StringBuilder();
		int length = randomString.length - 1;
		for (int i = 0 ; i < charCount ; i ++) {
			int index = getRandom(0,length);
			result.append(randomString[index]);
		}
		return result.toString();
	}

	/**
	 * 社会化显示时间差
	 * @param time Long
	 * @return String
	 */
	public String showTimeDiff(Long time) {
		if (null == time || time < 1) {
			return "";
		}
		long nowTime = ITimeUtils.gmTime();
		long timeDiff = nowTime - time;
		if (timeDiff <= 60L) {
			return "刚刚";
		} else if (timeDiff <= 3600L) {
			//一小时内
			long minutes = timeDiff / 60L;
			return minutes + "分钟前";
		} else if (timeDiff <= 86400L) {
			long hours = timeDiff / 3600L;
			long second = timeDiff - hours * 3600L;
			long minutes = second / 60L;
			return hours + "小时"+minutes+"分钟前";
		}
		return ITimeUtils.localDate(time);
	}

	/**
	 * 展示手机号
	 * @param mobile String
	 * @return String
	 */
	public String showMobile(String mobile) {
		return mobile.replaceAll("(\\d{3})\\d{4}(\\w{4})","$1***$2");
	}

	/**
	 * 计算两个坐标之间的距离 <a href="https://www.geodatasource.com/developers/java">来源链接</a>
	 * @param lon1 Double 坐标1经度
	 * @param lat1 Double 坐标1纬度
	 * @param lon2 Double 坐标2经度
	 * @param lat2 Double 坐标2纬度
	 * @param unit String 单位 where: 'M' is statute miles (default) 'K' is kilometers 'N' is nautical miles
	 * @return Double
	 */
	public Double distance(Double lon1,Double lat1, Double lon2, Double lat2, String unit) {
		if ((Objects.equals(lat1, lat2)) && (Objects.equals(lon1, lon2))) {
			return 0D;
		}
		else {
			double theta = lon1 - lon2;
			double dist = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta));
			dist = Math.acos(dist);
			dist = Math.toDegrees(dist);
			dist = dist * 60 * 1.1515;
			if (unit.equals("K")) {
				dist = dist * 1.609344;
			} else if (unit.equals("N")) {
				dist = dist * 0.8684;
			}
			return (dist);
		}
	}
}
